package MemberController;

import java.io.*;



import java.util.*;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import CommunityController.*;
import PhotoController.*;


public class MemberLogAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
	
    public MemberLogAction() {
        super();
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		loginProcess(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		loginProcess(request,response);
	}
	
	public void loginProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			System.out.println("*****로그인 시스템 이동******");
			
			
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			
			MembersBean mbean;
			MembersDAO mdao;
			
			PhotoYoungDAO photoYoungDAO=new PhotoYoungDAO();
			BCBoardDAO bCBoardDAO=new BCBoardDAO();
			FreeBoardDAO freeBoardDAO=new FreeBoardDAO();
			ArrayList list=photoYoungDAO.pt_young_list_main();
			ArrayList list2=freeBoardDAO.cm_free_list_main();
			ArrayList list3=bCBoardDAO.cm_bc_list_main();
			String resultString ="";
			RequestDispatcher view = null;
			PrintWriter out =response.getWriter();
			
			int result=0;
			int mcheck=0;
			boolean check = false;
			boolean check1 = false;
			
			String action_code = request.getParameter("action_code");
			
			
			mbean = new MembersBean();
			mdao = new MembersDAO();
			
			MLoginManager manager = MLoginManager.getInstance();
			
			if(action_code.equals("LevelCheck")){
				
				mbean.setMem_Nick(request.getParameter("nick"));
				result = mdao.mCheckLevel(mbean);
				
	 			resultString = null;			
	 			
				resultString = "<response><result>"+result+"</result></response>";
			    out = response.getWriter();
			    response.setContentType("text/xml;charset=utf-8");
			   
			    out.println(resultString);
			    out.close();
				
				System.out.println("*****이곳은 레벨체크구간입메다*****");
			
			
			}else if(action_code.equals("login")){
				
				String numMode = request.getParameter("num");
				int start = numMode != null ? Integer.parseInt(numMode) : -1;
				mbean.setMem_Nick(request.getParameter("Mem_Nick"));
				mbean.setMem_Pass(request.getParameter("Mem_Pass"));
				
			 switch(start){
				case 1:
					System.out.println("*****로그인검증시작******");
					
					System.out.println("아이디:"+mbean.getMem_Nick());
					System.out.println("비밀번호:"+mbean.getMem_Pass());
					
					check=mdao.mCheckId(mbean);
					check1=mdao.mCheckPass(mbean);
					
					System.out.println(check);
					System.out.println(check1);
					
					if(check == true && check1 == true){
						System.out.println("*****아이디/패스워드일치******");
							
						/*
							if(request.getParameter("Mem_Nick").equals("admin") && request.getParameter("Mem_Pass").equals("1111")){
								session = request.getSession();
								
								manager.setSession(session, mbean);
								session.setAttribute("Mem_Nick",request.getParameter("Mem_Nick"));
								view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
								view.forward(request, response);
								break;
							}
						*/
							
							String Mem_NameK = mdao.mCheckName(mbean);
							result=mdao.mCheckLevel(mbean);
							//int id = mdao.mIdCheck(request.getParameter("Mem_Nick"));
							
							session = request.getSession();
							manager.setSession(session, mbean);
							
							session.setAttribute("Mem_Nick",request.getParameter("Mem_Nick"));
							session.setAttribute("Mem_NameK",Mem_NameK);
							
							out.println( "<script>" );
							out.println( "location.href=\"MemberLogAction.do?action_code=sessioncheck&Mem_Nick="+request.getParameter("Mem_Nick")+"&result="+result+"\";" );
							out.println( "</script>" );
							break;
							
					}else if(check==false || check1==false) {
						System.out.println( "============================" );	
						System.out.println( "--아이디/패스워드 FAULT--" );
									
						out.println( "<script>" );
						out.println( "alert(\"ID/PASS また入力してください.\");" );
						out.println( "location.href=\"javascript:history.go(-1)\";" );
						out.println( "</script>" );
						break;
					}
					break;
				
				}
			}else if(action_code.equals("logindelete")){
				System.out.println("*****로그아웃시작*****");
				session = request.getSession();
				session.invalidate();
				
				out.println( "<script>" );
				out.println( "alert(\"ご利用していただいてありがとうございます.\");" );
			    out.println( "location.href=\"./index.jsp\";" );
				out.println( "</script>" );
			
			}else if(action_code.equals("sessioncheck")){
				
					System.out.println("집으로가자");
					System.out.println(request.getParameter("result"));					
					
					String Cresult = request.getParameter("result");
					String Mem_Nick = request.getParameter("Mem_Nick");
					System.out.println(request.getParameter("Mem_Nick"));
					
					session = request.getSession();
					
					if(Cresult.equals("1")){
						
						request.setAttribute("list",list);
						request.setAttribute("list2",list2);
						request.setAttribute("list3",list3);
						session.setAttribute("result",Cresult);
						view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");
						view.forward(request, response);
								
					}else if(Cresult.equals("2")){
							
							session.setAttribute("result",Cresult);
							out.println( "<script>" );
							out.println( "location.href=\"/KOMORIUTA/MYoungAction.do?\";" );
							out.println( "</script>" );
					}else if(Cresult.equals("3")){
						view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
						view.forward(request, response);
					}
			}else if(action_code.equals("postcheck")){
				System.out.println("*****우편번호검색시작*****");
				String find = "";
				String town = request.getParameter("zip_nm");
				System.out.println("마을 이름:"+town);
				ZipcodeBean zbean = new ZipcodeBean();
				
				find = mdao.mCheckTown(town);
				System.out.println("찾았니?"+find);
				ArrayList beanList=mdao.memZipcheck(town);
				
				request.setAttribute("find",find);
				request.setAttribute("resultList",beanList);
				request.setAttribute("town",town);
				
				System.out.println("*****우편번호검색종료*****");
				
				view = request.getRequestDispatcher("/Main/FirstMain/Fm_zipcode.jsp");
				view.forward(request, response);
			
			}else if(action_code.equals("gest")){
				String num = request.getParameter("num");
				
				if(num.equals("old")){
					
					session = request.getSession();
					
					request.setAttribute("list",list);
					request.setAttribute("list2",list2);
					request.setAttribute("list3",list3);
					session.setAttribute("Mem_Nick", "Guest");
					
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");
					view.forward(request, response);
			System.out.println("*****난 늙었다.*****");
				}else if(num.equals("young")){
					
					session = request.getSession();
					session.setAttribute("Mem_Nick", "Guest");
					
					out.println( "<script>" );
					out.println( "location.href=\"/KOMORIUTA/MYoungAction.do?\";" );
					out.println( "</script>" );
			System.out.println("*****난 젊다.*****");					
				}
			}
		}
	}
